package cn.dujc.superkid.activities;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseActivity;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.RecordsAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.ChannelEntity;
import cn.dujc.superkid.bean.ConsumptionResult;
import cn.dujc.superkid.bean.GoodQuestionResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.utils.Preferences;
import cn.dujc.superkid.widgets.EmptyView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2018/10/22 0022.
 */

public class ActivityRecords extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {
    RecyclerView recyclerView;
    RecordsAdapter adapter;
    private int page;

    @Override
    public int getViewId() {
        return R.layout.activity_records;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        setTitle("消费记录");
        recyclerView = findViewById(R.id.records_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RecordsAdapter(R.layout.item_records, null);
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(this,recyclerView);
        loadData();
    }

    public void loadData() {
        final ChannelEntity channel = Preferences.getInstance().getChannel(mActivity);
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);

        HttpHelper.getUserCenter().memberConsumption(QMap.build()
                .put("channel", channel.getChannel_id())
                .put("num", "20")
                .put("logincode", loginResult.getLogincode())
                .put("user_id", loginResult.getUid())
                .put("page", page + "")
                .create()).enqueue(new Callback<Result<ConsumptionResult>>() {
            @Override
            public void onResponse(Call<Result<ConsumptionResult>> call, Response<Result<ConsumptionResult>> response) {
                List<ConsumptionResult.OrderEntity> data = response.body().getData().getOrder();
                if (page == 0) {
                    adapter.setNewData(data);
                    if(data.size()==0){
                        adapter.setEmptyView(R.layout.layout_empty_img);
                    }
                } else {
                    adapter.addData(data);
                    adapter.loadMoreComplete();
                }
                if (data.size() < 20) {
                    adapter.loadMoreEnd(true);
                }
            }

            @Override
            public void onFailure(Call<Result<ConsumptionResult>> call, Throwable t) {
            }
        });

    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        loadData();
    }
}
